package com.pignest.hotel.module.booking;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Vbox;

import com.pignest.hotel.beans.Booking;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.BookingStatus;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.ui.room.RoomHistogram;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

@Deprecated
public class BookingForm extends Vbox {
	private static final long serialVersionUID = 735469555202904368L;

//	private Listbox bookingType;
//	private Listbox bookingMethod;
	private Textbox userName;
	private Textbox phoneNumber;
	private Datebox estimatedDate;
	private Listbox estimatedTime;
	private Listbox preserve;
	private Textbox remark;
	
	private List<Room> rooms;
	
	private void init() {
		if (preserve != null)
			return;
//		bookingType = (Listbox)getFellow("bookingType");
//		bookingMethod = (Listbox)getFellow("bookingMethod");
		userName = (Textbox)getFellow("userName");
		phoneNumber = (Textbox)getFellow("phoneNumber");
		estimatedDate = (Datebox)getFellow("estimatedDate");
		estimatedTime = (Listbox)getFellow("estimatedTime");
		preserve = (Listbox)getFellow("preserve");
		remark = (Textbox)getFellow("remark");
	}
	
	public boolean check() {
		init();
		String name = userName.getValue();
		String phone = phoneNumber.getValue();
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone)) {
			userName.focus();
			UIUtils.alert("请输入预订者姓名和电话!");
			return false;
		}
		Date d = estimatedDate.getValue();
		if (!estimatedDate.isValid() || d == null || d.getTime() < System.currentTimeMillis() - 24L * 3600 * 1000) {
			estimatedDate.focus();
			UIUtils.alert("请输入合法的预订时间!, 预订日期晚于现在.");
			return false;
		}
		return true;
	}
	
	public void addRoom(Room room) {
		if (rooms == null)
			rooms = new ArrayList<Room>();
		rooms.add(room);
	}
	
	public void booking(RoomHistogram histogram) {
		if (!check())
			return;
		String name = userName.getValue();
		String phone = phoneNumber.getValue();
		BookingStatus status = BookingStatus.ONGOING;
//		String method = UIUtils.getSelectedValue(bookingMethod);
		Date date = estimatedDate.getValue();
		String time = UIUtils.getSelectedValue(estimatedTime);
		int[] hm = DateUtil.parseHMS(time);
		
		Booking booking = new Booking();
		booking.setName(name);
		booking.setPhone(phone);
		booking.setStatus(status.getDisplayName());
		booking.setRemark(remark.getValue());
		booking.setTime(new Date());
		if (hm != null) {
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
			cal.set(Calendar.HOUR, hm[0]);
			cal.set(Calendar.MINUTE, hm[1]);
			date = cal.getTime();
		}
		booking.setEstimatedTime(date);
		int p = Integer.parseInt((String)UIUtils.getSelectedValue(preserve));
		booking.setEndTime(new Date(date.getTime() + p * 60 * 1000));
		
		ReceptionService rs = Services.getReceptionService();
		List<String> roomIds = histogram.getSelected();
		rs.addBooking(booking, roomIds);
	}
	
}
